package com.fudan.es.aggs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fudan.biz.data.service.FieldMetaService;
import com.fudan.es.aggs.entity.DimensionDef;
import com.fudan.es.entity.DimensionPack;
import com.fudan.es.entity.FieldMeta;

@Component
public class DimensionParser {

	@Autowired
	private FieldMetaService fieldMetaService;

	public DimensionPack build(DimensionDef dimension) {
		String type = dimension.getType();
		String fieldId = dimension.getField();
		FieldMeta field = fieldMetaService.get(fieldId);
		DimensionType dimensionType = DimensionType.valueOf(type.toUpperCase());
		DimensionPack dimensionPack = DimensionPack.build(field, dimensionType,dimension);
		setConfig(dimension,dimensionPack);
		return dimensionPack;
	}

	public static enum DimensionType {
		TERMS,DATE,FILTERS;

		@Override
		public String toString() {
			return name().toLowerCase();
		}
	}

	private void setConfig(DimensionDef dimensionDef,DimensionPack dimensionPack) {
		dimensionPack.setSize(dimensionDef.getSize());
		dimensionPack.setMissing(dimensionDef.getMissing());
		dimensionPack.setOther(dimensionDef.getOther());
		dimensionPack.setInterval(dimensionDef.getInterval());
	}
}
